<?php
defined('BASEPATH') OR exit('No direct script access allowed');

function genUUID() {
    $uuid = '';
    list($mic, $sec) = explode(' ', microtime());
    $mic  *= 100000000;
    $mic  = dechex($mic);
    $sec  = dechex($sec);
    $mic  = ensureHex($mic, 5);
    $sec  = ensureHex($sec, 8);

    $uuid = $mic.genRand(3).genRand(4).genRand(4).genRand(4).$sec.genRand(4);
    return $uuid;
}

function ensureHex($hex, $length) {
    $len = strlen($hex);
    if ($len<$length) {
        for ($i=0; $i!=($length-$len); ++$i) {
            $num = dechex(mt_rand(0, 15));
            $hex = $num.$hex;
        }
    } else if ($len>$length) {
        $hex = substr($hex, $len-$length, $length);
    }
    return $hex;
}

function genRand($length) {
    $str = '';
    for ($i=0; $i!=$length; ++$i) {
        $num = dechex(mt_rand(0, 15));
        $str .= $num;
    }
    return $str;
}

function outputError($errorCode = 404, $errorMsg = '') {
    header('Content-Type: application/json; charset=UTF-8');
    $data = array(
        'code'    => $errorCode,
        'message' => $errorMsg
    );
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit();
}

function outputJson($code, $content) {
    header('Content-Type: application/json; charset=UTF-8');
    $data = array(
        'code' => $code,
        'data' => $content
    );
    echo json_encode($data);
    exit();
}

function textFilter($text) {
    preg_match_all('/[0-9a-zA-Z\x{4e00}-\x{9fa5}\x{00}-\x{ff}\x{20}-\x{7f}\x{a1}-\x{ff}\x{80}-\x{ff}\x{3130}-\x{318F}\x{AC00}-\x{D7A3}\x{0800}-\x{4e00}\s]/us', $text, $matches);
    if ($matches) {
        $text = $matches[0];
        $text = implode('', $text);
    }
    $text = htmlspecialchars(addslashes(trim($text)));
    $text = str_replace('--', '', $text);
    return $text;
}

function httpGet($url, $timeout = 10, $options = array()) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    if ($options) {
        curl_setopt_array($ch, $options);
    }

    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}

function httpPost($url, $post, $timeout = 10, $options = array()) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPGET, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    if ($options) {
        curl_setopt_array($ch, $options);
    }

    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}

function httpPut($url, $post, $timeout = 10, $options = array()) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPGET, FALSE);
    curl_setopt($ch, CURLOPT_POST, FALSE);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('X-HTTP-Method-Override: PUT'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    if ($options) {
        curl_setopt_array($ch, $options);
    }

    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}

function httpCGet($url, $post, $timeout = 10, $options = array()) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    if ($options) {
        curl_setopt_array($ch, $options);
    }

    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}
